import { closeOrder,receiptOrder } from "@/api/store/order";
import { Result, ResultError } from "@/types";
import { ElMessageBox } from "element-plus";
import { Ref, ref, reactive, watch, getCurrentInstance } from "vue";
export default function (_call: any) {

    const { proxy } = getCurrentInstance() as any;

    const {
        proxy: { $utils },
    } = getCurrentInstance() as any;

    //修改收货地址
    const _toUpdateAddress = (id: number) => {
        proxy?.$refs["updateAddressRef"].open(id);
    };

    //线下支付
    const _toOfflinePayment = (id: number) => {
        proxy?.$refs["offlinePaymentRef"].open(id);
    };

    //订单发货
    const _toOrderDelivery = (id: number) => {
        proxy?.$refs["orderDeliveryRef"].open(id);
    };

    //调整价格
    const _toUpdatePrice = (id: number) => {
        proxy?.$refs["updatePriceRef"].open(id);
    };

    const _toOrderCancel = (id: number) => {
        proxy?.$refs["orderCancelRef"].open(id);
    }


    //关闭订单
    const _onCloseOrder = (id: number) => {
        ElMessageBox.confirm("确定要关闭订单吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
        }).then(() => {
            closeOrder({
                id,
            })
                .then((res: Result) => {
                    if (typeof _call == "function") {
                        _call()
                    }
                    $utils.successMsg(res);
                })
                .catch((err: ResultError) => {
                    $utils.errorMsg(err);
                });
        });
    };

    //核销
    const _onExtract = (id: number) => {
        proxy?.$refs["orderExtractRef"].open(id);
    };

    //备注
    const _onRemark = (id: number) => {
        proxy?.$refs["orderRemarkRef"].open(id);
    }

    //确定收货
    const _onReceiptOrder = (id: number) => {
        ElMessageBox.confirm("确定客户已收到货吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
        }).then(() => {
            receiptOrder({
                id,
            })
                .then((res: Result) => {
                    if (typeof _call == "function") {
                        _call()
                    }
                    $utils.successMsg(res);
                })
                .catch((err: ResultError) => {
                    $utils.errorMsg(err);
                });
        });
    }

    return {
        _toUpdateAddress,
        _toOfflinePayment,
        _toOrderDelivery,
        _toUpdatePrice,
        _onCloseOrder,
        _onExtract,
        _toOrderCancel,
        _onRemark,
        _onReceiptOrder
    };
}